/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

 // vars
 var activeSong;
 var controlStopRadio = 0;
 //var urlRadio = 'http://radioshalomfm.servehttp.com/live';

var app = {
    // Application Constructor
    initialize: function() 
    {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() 
    {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() 
    {
        FastClick.attach(document.body);
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) 
    {
        iniciarApp();
    }
};

function iniciarApp()
{
    var resultAppStatus = appStatus();
    if (resultAppStatus == 1)
    {
        crearRadio();
        inicio();   
    }
}

function appStatus()
{

    var appStatus = window.localStorage.getItem("appStatus");   // Control Status App 0 Primera Vez 1 Ya iniciada
    if (appStatus == null)
    {        
        alert('Iniciar Variable')
        window.localStorage.setItem("appStatus", 1);   // Control Iniciamos la app
        return 1;
    }
    else
    {        
        return 1;
    } 
}

function crearRadio() {
  try {        
    activeSong = document.createElement('audio');
  } catch (e) {
    alert('no audio support!');
  } 
}


/*:::::::::::::::::::::::::::
:::::::::: Inicio :::::::::::
:::::::::::::::::::::::::::*/
function inicio()
{
    // Ocultar Radio
    viewCloseRadio(0);
    // Loading
    loading('container');
    // Iconos    
    /*var contentIconos = 'Iconos';
    var container = document.getElementById('iconos');
    container.innerHTML = contentIconos; */

    // Contenido
    var contentInicio = '';
    contentInicio += '<table width="100%" height="100%">';
    contentInicio += '  <tbody>';
    contentInicio += '      <tr class="mensajeBiblicoTr">';
    contentInicio += '          <td colspan="2">';
    contentInicio += '              <div class="mensajeBiblico">';
    contentInicio += '                  <span class="capitulo left">Filipenses 4:7 | </span>';
    contentInicio += '                  <span class="versiculo">Y la paz de Dios, que sobrepasa todo entendimiento, guardará vuestros corazones y vuestros pensamientos en Cristo Jesús.</span>';
    contentInicio += '              </div>';
    contentInicio += '          </td>';
    contentInicio += '      </tr>';
    contentInicio += '      <tr class="colorAzul">';
    contentInicio += '          <td onclick="radio();" class="center tdRadio"><div class="imgRadio"><i class="fa fa-headphones"></i></div></td>';
    contentInicio += '          <td onclick="television();" class="center tdTelevision"><div class="imgTelevision"><i class="fa fa-desktop"></i></div></td>';
    contentInicio += '      </tr>';
    contentInicio += '      <tr class="colorAzul">';
    contentInicio += '          <td onclick="noticias();" class="center tdNoticias"><div class="imgNoticias"><i class="fa fa-rss"></i></div></td>';
    contentInicio += '          <td onclick="predicas();" class="center tdPredicas"><div class="imgPredicas"><i class="fa fa-book"></i></div></td>';
    contentInicio += '      </tr>';
    contentInicio += '  </tbody>';
    contentInicio += '</table>';

    var container = document.getElementById('container');
    container.innerHTML = contentInicio; 

    // Cerramos Menu
    menuAction(0);
}

/*:::::::::::::::::::::::::::
:::::::::: Radio :::::::::::
:::::::::::::::::::::::::::*/
function radio()
{
    // Loading
    loading('container');
    // Iconos    
    /*var contentIconos = 'Iconos';
    var container = document.getElementById('iconos');
    container.innerHTML = contentIconos; */

    // Contenido
    var contentRadio = '<div class="fondoRadio">';
    contentRadio += '';
    contentRadio += '</div>';
    var container = document.getElementById('container');
    container.innerHTML = contentRadio; 
    // mostrar Radio
    viewCloseRadio(1);

    // Cerramos Menu
    menuAction(0);
}

function viewCloseRadio(control)
{
    if (control == 1)        
        document.getElementById('reproductorShalom').style.display = "block";
    else if (control == 0)
        document.getElementById('reproductorShalom').style.display = "none";
}


function procesarRadio(resp)
{    
    var urlRadio;
    switch(resp) {
        case 0:         
            urlRadio = 'http://stop.com:8000/live.mp3';
            controlStopRadio = 0;  
            document.getElementById('songPlay').setAttribute('onclick','procesarRadio(1)'); 
            document.getElementById('songPlay').setAttribute('class','fa fa-play buttonPlay');         
            play(urlRadio);
            document.getElementById('songPlay').setAttribute('style','display: block !important');
            document.getElementById('songPause').setAttribute('style','display: none !important');
            break;
        case 1:         // Radio Shalom Play
            controlStopRadio = 1;
            urlRadio = 'http://revolutionradio.ru:8000/live.mp3';
            document.getElementById('songPlay').setAttribute('onclick','procesarRadio(2)');
            play(urlRadio);  
            document.getElementById('songPlay').setAttribute('style','display: none !important');
            document.getElementById('songPause').setAttribute('style','display: block !important');
            break;
        case 2:         // Radio Shalom Pause
            controlStopRadio = 2;            
            document.getElementById('songPlay').setAttribute('onclick','procesarRadio(3)');
            document.getElementById('songPlay').setAttribute('class','fa fa-play buttonPlay parpadeo');
            pause(urlRadio);
            document.getElementById('songPlay').setAttribute('style','display: block !important');
            document.getElementById('songPause').setAttribute('style','display: none !important');
            break;
        case 3:         // Radio Shalom Play sin url
            controlStopRadio = 2;            
            document.getElementById('songPlay').setAttribute('onclick','procesarRadio(3)');
            playp();
            document.getElementById('songPlay').setAttribute('style','display: none !important');
            document.getElementById('songPause').setAttribute('style','display: block !important');
            break;
        default:
            urlRadio = 'http://revolutionradio.ru:8000/live.mp3';        
    }
}

/*:::::::::::::::::::::::::::
:::::::::: Television :::::::::::
:::::::::::::::::::::::::::*/
function television()
{
    if (controlStopRadio != 0)
    {        
        stopSong();
    }

    // Ocultar Radio
    viewCloseRadio(0);
    // Loading
    loading('container');
    // Iconos    
    /*var contentIconos = 'Iconos';
    var container = document.getElementById('iconos');
    container.innerHTML = contentIconos; */

    // Contenido
    var contentInicio = '<iframe src="http://www.ustream.tv/embed/4172518?wmode=direct" style="border: 0 none transparent;" frameborder="no" width="100%" height="100%"></iframe><br /><a href="http://www.ustream.tv/" style="padding: 2px 0px 4px; width: 100%; background: #ffffff; display: block; color: #000000; font-weight: normal; font-size: 10px; text-decoration: underline; text-align: center;" target="_blank">Live streaming video by Ustream</a>';

    var container = document.getElementById('container');
    container.innerHTML = contentInicio; 

    // Cerramos Menu
    menuAction(0);
}

/*:::::::::::::::::::::::::::
:::::::::: Noticias :::::::::::
:::::::::::::::::::::::::::*/
function noticias()
{
    // Ocultar Radio
    viewCloseRadio(0);
    // Loading
    loading('container');
    // Iconos    
    /*var contentIconos = 'Iconos';
    var container = document.getElementById('iconos');
    container.innerHTML = contentIconos; */

    // Contenido
    var contentInicio = 'Menu Noticias En construccion...';

    var container = document.getElementById('container');
    container.innerHTML = contentInicio; 

    // Cerramos Menu
    menuAction(0);
}

/*:::::::::::::::::::::::::::
:::::::::: Predicas :::::::::::
:::::::::::::::::::::::::::*/
function predicas()
{
    // Ocultar Radio
    viewCloseRadio(0);
    // Loading
    loading('container');
    // Iconos    
    /*var contentIconos = 'Iconos';
    var container = document.getElementById('iconos');
    container.innerHTML = contentIconos; */

    // Contenido
    var contentInicio = 'Menu Predicas En construccion...';

    var container = document.getElementById('container');
    container.innerHTML = contentInicio; 

    // Cerramos Menu
    menuAction(0);
}

/*:::::::::::::::::::::::::::
:::::::::: Funtion :::::::::::
:::::::::::::::::::::::::::*/
function generica()
{
    // Loading
    loading('container');
    // Iconos    
    /*var contentIconos = 'Iconos';
    var container = document.getElementById('iconos');
    container.innerHTML = contentIconos; */

    // Contenido
    var contentInicio = 'generica';

    var container = document.getElementById('container');
    container.innerHTML = contentInicio; 
}

function loading(element)
{
    // Loading
    var container = document.getElementById(element);
    container.innerHTML = '<div align="center" class="loading"><i class="fa fa-spinner fa-spin"></i></div>';
}

function menuAction(action)
{
    if ( action == 1 )
        document.getElementById('Menu').style.display = "block";
    else
    {
        document.getElementById('Menu').style.display = "none";        
    }
}